/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EmployeeWorkStagnationPanel.java
 *
 * Created on Jul 5, 2010, 7:05:53 PM
 */

package org.foment.gem.beans;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import org.foment.gem.dialogs.EmployeesDialog;
import org.foment.joliage3.sql.Database;

/**
 *
 * @author Dusan
 */
public class EmployeeWorkStagnationPanel extends javax.swing.JPanel implements java.io.Serializable {

    /** Creates new form EmployeeWorkStagnationPanel */
    public EmployeeWorkStagnationPanel() {
        initComponents();
        setComponents();
    }

    private void setComponents()
    {
        listWorkStagnation.listenTables("EmployeeWorkStagnation");
    }
    public void update() throws SQLException
    {
        _currentEmployee = Database.getTable("Employee").getResultSet().getInt("EmployeeID");
        String query = "SELECT EmployeeWorkStagnationID as keyName, CONCAT(DATE(EmployeeWorkStagnationStartDate),' do '," +
                "DATE(EmployeeWorkStagnationEndDate)) as fieldName"  +
                " FROM EmployeeWorkStagnation WHERE EmployeeID = '" + _currentEmployee + "' ;";

        listWorkStagnation.setQuery(query);
        startDate.setValue(null); // praznimo polja za datum
        endDate.setValue(null);
    }

    public long getTotalStagnation()
    {
        String query = "SELECT EmployeeWorkStagnationID, EmployeeWorkStagnationStartDate as sdate, " +
                "EmployeeWorkStagnationEndDate as edate FROM EmployeeWorkStagnation WHERE EmployeeID = '" + _currentEmployee + "' ;";

        long delay = 0; // ukupna pauza u milisekundama
        Date sdate = new Date();
        Date edate = new Date();
        try
        {
            Connection  conn = Database.getConnection();
            ResultSet rs = conn.createStatement().executeQuery(query);
            while(rs.next())
            {
                sdate = rs.getDate("sdate");
                edate = rs.getDate("edate");
                delay += (edate.getTime() - sdate.getTime());
            }
            rs.close();
            conn.close();
        }
        catch (SQLException e) {e.printStackTrace();}
        //System.out.println("Calculating for " + _currentEmployee);
        return delay;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listWorkStagnation = new org.foment.joliage3.newt.SQLListBox();
        startDate = new org.foment.joliage3.newt.DateField();
        endDate = new org.foment.joliage3.newt.DateField();
        btnAdd = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Stagnacija staza"));

        jScrollPane1.setViewportView(listWorkStagnation);

        btnAdd.setText("+");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnRemove.setText("-");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnRemove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(endDate, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                    .addComponent(startDate, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnAdd)
                            .addComponent(startDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnRemove)
                            .addComponent(endDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (startDate.getValue() == null || endDate.getValue() == null || startDate.getValue().after(endDate.getValue()))
        {
            JOptionPane.showMessageDialog(this, "Nekorektan izbor datuma");
            return;
        }
        String sdate = new java.text.SimpleDateFormat("yyyy-MM-dd").format(startDate.getValue());
        String edate = new java.text.SimpleDateFormat("yyyy-MM-dd").format(endDate.getValue());

        String query = "INSERT INTO EmployeeWorkStagnation (EmployeeID, EmployeeWorkStagnationStartDate, " +
                " EmployeeWorkStagnationEndDate ) VALUES ('" + _currentEmployee + "', '" + sdate + "', '" +
                edate + "')";
        try
        {
            Connection  conn = Database.getConnection();
            conn.createStatement().executeUpdate(query);
            conn.close();
            Database.getTable("EmployeeWorkStagnation").reload(); // ovo ce osveziti i listu
        }
        catch (SQLException e) {e.printStackTrace();}
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
        // TODO add your handling code here:
        int position = listWorkStagnation.getValue();
        if (position < 0 || _currentEmployee < 0)
            return;

        String query = "DELETE FROM EmployeeWorkStagnation WHERE " +
                "EmployeeWorkStagnationID = '" + position + "' ;";
        try
        {
            Connection  conn = Database.getConnection();
            conn.createStatement().executeUpdate(query);
            conn.close();
            Database.getTable("EmployeeWorkStagnation").reload();
        }
        catch(SQLException e) {e.printStackTrace();}
    }//GEN-LAST:event_btnRemoveActionPerformed

    @Override
    public void setEnabled(boolean flag)
    {
        super.setEnabled(flag);
        for (Component c : getComponents())
            c.setEnabled(flag);
    }

    public void clear()
    {
        startDate.setValue( null );
        endDate.setValue( null );
        listWorkStagnation.setModel( voidModel );
    }

    public JButton getAddButton()
    {
        return btnAdd;
    }
    private Date _startDate = new Date();
    private Date _endDate = new Date();
    private int _currentEmployee;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnRemove;
    private org.foment.joliage3.newt.DateField endDate;
    private javax.swing.JScrollPane jScrollPane1;
    private org.foment.joliage3.newt.SQLListBox listWorkStagnation;
    private org.foment.joliage3.newt.DateField startDate;
    // End of variables declaration//GEN-END:variables

    DefaultListModel voidModel = new DefaultListModel();
}
